from odoo import fields, models, api


class ProductLock(models.Model):
    _inherit = 's.product.lock'
    _order = 'sale_order_line_id, forecast_order_line_id, product_id'

    partner_id = fields.Many2one('res.partner', 'Customer', compute='_compute_partner_id', store=True)
    sale_user_id = fields.Many2one('res.users', 'Sale user', compute='_compute_partner_id', store=True)
    forecast_order_id = fields.Many2one('s.forecast.order', string='Forecast Order')
    forecast_order_line_id = fields.Many2one('s.forecast.order.line', string='Forecast Order')

    preparation_id = fields.Many2one('s.sale.order.material.preparation.wizard')

    @api.depends('sale_id', 'forecast_order_id')
    def _compute_partner_id(self):
        for rec in self:
            if rec.sale_id:
                rec.partner_id = rec.sale_id.partner_id.id
                rec.sale_user_id = rec.sale_id.user_id.id
            elif rec.forecast_order_id:
                rec.partner_id = rec.forecast_order_id.customer_id.id
                rec.sale_user_id = rec.forecast_order_id.responsible_id.id
            else:
                rec.partner_id = False
                rec.sale_user_id = False
